{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Dtype\n",
    "## Data Type Object\n",
    "Let's look into how you might generate positions from signals. To do that, we first need to know about `dtype` or data type objects in Numpy.\n",
    "\n",
    "A [data type object](https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.dtypes.html) is a class that represents the data. It's similar to a [data type](data type), but contains [more information](https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.dtypes.html) about the data. Let's see an example of a data type object in Numpy using the array `array`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "array = np.arange(10)\n",
    "\n",
    "print(array)\n",
    "print(type(array))\n",
    "print(array.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "From this, we see `array` is a `numpy.ndarray` with the data `[0 1 2 3 4 5 6 7 8 9]` represented as `int64` (64-bit integer).\n",
    "\n",
    "Let's see what happens when we divide the data by 2 to generate not integer data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "float_arr = array / 2\n",
    "\n",
    "print(float_arr)\n",
    "print(type(float_arr))\n",
    "print(float_arr.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The array returned has the values `[ 0.   0.5  1.   1.5  2.   2.5  3.   3.5  4.   4.5]`, which is what you would expect for divinding by 2. However, since this data can't be represeted by integers, the array is now represented as `float64` (64-bit float).\n",
    "\n",
    "How would we convert this back to `int64`? We'll use the [`ndarray.astype`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html) function to cast it from it's current type to the type of `int64` (`np.int64`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "int_arr = float_arr.astype(np.int64)\n",
    "\n",
    "print(int_arr)\n",
    "print(type(int_arr))\n",
    "print(int_arr.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This casts the data to `int64`, but all also changes the data. Since fractions can't be represented as integers, the decimal place is dropped.\n",
    "\n",
    "## Signals to Positions\n",
    "Now that you've seen how the a [data type object](https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.dtypes.html) is used in Numpy, let's see how to use it to generate positions from signals. Let's use `prices` array to represent the prices in dollars over time for a single stock."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "prices = np.array([1, 3, -2, 9, 5, 7, 2])\n",
    "\n",
    "prices"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For the positions, let's say we want to buy one share of stock when the price is above 2 dollars and the buy 3 more shares when it's above 4 dollars. We'll first need to generate the signal for these two positions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "signal_one = prices > 2\n",
    "signal_three = prices > 4\n",
    "\n",
    "print(signal_one)\n",
    "print(signal_three)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This gives us the points in time for the signals above 2 dollars and above 4 dollars. To turn this into positions, we need to multiply each array by the respective amount to invest. We first need to turn each signal into an integer using the [`ndarray.astype`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html) function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "signal_one = signal_one.astype(np.int)\n",
    "signal_three = signal_three.astype(np.int)\n",
    "\n",
    "print(signal_one)\n",
    "print(signal_three)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we multiply each array by the respective amount to invest. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pos_one = 1 * signal_one\n",
    "pos_three = 3 * signal_three\n",
    "\n",
    "print(pos_one)\n",
    "print(pos_three)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we add them together, we have the final position of the stock over time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "long_pos = pos_one + pos_three\n",
    "\n",
    "print(long_pos)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quiz\n",
    "Using this information, implement `generate_positions` using Pandas's [`df.astype`](https://pandas.pydata.org/pandas-docs/version/0.21/generated/pandas.DataFrame.astype.html) function to convert `prices` to final positions using the following signals:\n",
    "- Long 30 share of stock when the price is above 50 dollars\n",
    "- Short 10 shares of stock when it's below 20 dollars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import project_tests\n",
    "\n",
    "\n",
    "def generate_positions(prices):\n",
    "    \"\"\"\n",
    "    Generate the following signals:\n",
    "     - Long 30 share of stock when the price is above 50 dollars\n",
    "     - Short 10 shares when it's below 20 dollars\n",
    "    \n",
    "    Parameters\n",
    "    ----------\n",
    "    prices : DataFrame\n",
    "        Prices for each ticker and date\n",
    "    \n",
    "    Returns\n",
    "    -------\n",
    "    final_positions : DataFrame\n",
    "        Final positions for each ticker and date\n",
    "    \"\"\"\n",
    "    # TODO: Implement Function\n",
    "    \n",
    "    return None\n",
    "\n",
    "\n",
    "project_tests.test_generate_positions(generate_positions)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quiz Solution\n",
    "If you're having trouble, you can check out the quiz solution [here](dtype_solution.ipynb)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
